﻿using Liu_Project.Entity.Admin;
using LSL.SqlClient;
using Liu_Project.Utils;
using System;
using System.Web;
using System.Web.SessionState;

namespace Liu_Project.Web.Admin.Handler
{
    /// <summary>
    /// LoginHand 的摘要说明
    /// </summary>
    public class LoginHand : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string Act = context.Request["method"];
            switch (Act.ToLower())
            {
                case "login":
                    Login(context);
                    break;
                default:
                    context.Response.Write(CommonFunction.ErrorJson("没有找到对应的处理方法."));
                    break;
            }
        }


        public void Login(HttpContext _context)
        {
            try
            {
                string txtuserNo = DataConvert.ToString(_context.Request["txtuserNo"]);
                string txtuserPwd = DataConvert.ToString(_context.Request["txtuserPwd"]);
                string txtvalidate = DataConvert.ToString(_context.Request["txtvalidate"]);

                if (_context.Session["CheckCode"] == null)
                {
                    _context.Response.Write(CommonFunction.ErrorJson("验证码已过期"));
                    return;
                }
                else if (txtvalidate.ToLower() != _context.Session["CheckCode"].ToString())
                {
                    _context.Response.Write(CommonFunction.ErrorJson("验证码错误"));
                    return;
                }

                var dal = new BaseDAL();

                var user = dal.GetEntityByField<EntUser>("UserNo", txtuserNo);

                if (user != null)
                {
                    var localIp = IPHelper.GetLocalIp();
                    var extranetIp = IPHelper.GetExtranetIp();
                    var clientIp = IPHelper.GetClientIP();
                    
                    var itemLoginLog = new EntUserLoginLog
                    {
                        UserId = user.UserId,
                        LoginTime = DateTime.Now,
                        LocalIp = localIp,
                        ExtranetIp = extranetIp,
                        ClientIP = clientIp,
                        Browser = _context.Request.Browser.Browser,
                        IpAddress = IPHelper.GetBaiduIpAddress(clientIp),
                    };

                    int code = 0;
                    string result = string.Empty;
                    if (!user.IsVisiable)
                    {
                        code = 1;
                        itemLoginLog.IsSuccess = false;
                        result = "当前用户已禁用,请联系管理员.";
                    }
                    else
                    {
                        if (Function.GetMD5(txtuserPwd) == user.Password)
                        {
                            itemLoginLog.IsSuccess = true;

                            var userRole = dal.GetEntityByField<EntRoleUser>("UserId", user.UserId.ToString());

                            user.EntRoleUser = userRole;
                            user.LastLoginTime = DateTime.Now;
                            dal.UpdateByEntity(user);

                            _context.Session["CurrentUser"] = user;
                            result = "校验通过.";
                        }
                        else
                        {
                            itemLoginLog.IsSuccess = false;
                            result = "密码错误";
                        }
                    }
                    dal.InsertByEntity(itemLoginLog);
                    if (itemLoginLog.IsSuccess)
                        _context.Response.Write(CommonFunction.SuccessJson(result));
                    else
                        _context.Response.Write(CommonFunction.ErrorJson(result, code));
                }
                else
                {
                    _context.Response.Write(CommonFunction.ErrorJson("用户名不存在"));
                }

            }
            catch (Exception ex)
            {
                LogFunction.WriteLog("LoginHand-Login", ex);
                _context.Response.Write(CommonFunction.ErrorJson(ex.Message.ToString()));
            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}